package com.softtown.modules.workflow.service.impl;

import com.softtown.common.enums.*;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.enums.SwfAprvDealTypeEnum;
import com.softtown.enums.SwfAprvEnum;
import com.softtown.enums.SwfTaskEnum;
import com.softtown.modules.message.utils.SendMessageUtil;
import com.softtown.modules.organization.entity.CompOrgDeptEntity;
import com.softtown.modules.organization.entity.CompOrgEmplEntity;
import com.softtown.modules.organization.service.CompOrgEmplService;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.entity.CompSysParamEntity;
import com.softtown.modules.sys.entity.CompSysUserEmplEntity;
import com.softtown.modules.sys.enums.AprvResultEnum;
import com.softtown.modules.sys.enums.AprvStatusEnum;
import com.softtown.modules.sys.enums.StatusEnum;
import com.softtown.modules.sys.service.CompSysUserEmplService;
import com.softtown.modules.workflow.dao.SwfProcInstDao;
import com.softtown.modules.workflow.entity.*;
import com.softtown.modules.workflow.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Dingyayong
 * @Date: 2019/8/21 19:50
 * @Description:
 */
@Service
public class SwfProcInstServiceImpl extends BaseServiceImpl<SwfProcInstDao, SwfProcInstEntity> implements SwfProcInstService {
    @Resource
    private CompOrgEmplService compOrgEmplService;

    @Resource
    private SwfDocPhaseDefService swfDocPhaseDefService;

    @Resource
    private SwfProcPhaseService swfProcPhaseService;

    @Resource
    private SwfTaskService swfTaskService;

    @Resource
    private SwfPhaseNameDefService swfPhaseNameDefService;

    @Resource
    private SwfHstAprvService swfHstAprvService;

    @Resource
    private SwfNextAprvDefService swfNextAprvDefService;

    @Resource
    private CompSysUserEmplService compSysUserEmplService;

    @Resource
    private SendMessageUtil sendMessageUtil;

    /**
     * 通过公司id和申请单id找出对应的唯一流程实例
     *
     * @param compId 公司id
     * @param applId 申请单id
     * @return com.softtown.modules.entity.SwfProcInstEntity
     * @Author Dingyayong
     * @Date 2020/4/13 19:44
     */
    @Override
    public SwfProcInstEntity getSwfProcInst(Long compId, Long applId) {
        return baseDao.getSwfProcInst(compId, applId);
    }

    /**
     * 通过id找出对应的唯一流程实例
     *
     * @param id id
     * @return com.softtown.modules.entity.SwfProcInstEntity
     * @Author Dingyayong
     * @Date 2020/4/13 19:45
     */
    @Override
    public SwfProcInstEntity getSwfProcInstById(Long id) {
        return baseDao.getSwfProcInstById(id);
    }

    /**
     * 更新流程实例
     *
     * @param swfProcInstEntity 流程实例实体类
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 19:46
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSwfProcInst(SwfProcInstEntity swfProcInstEntity) {
        baseDao.updateSwfProcInst(swfProcInstEntity);
    }






    private SwfProcInstEntity refuse(Long aprvProcId,Long compId,String aprvComment,Long applId){
        Date nowDate = new Date();
        Long userId = SecurityUser.getUser().getId();
        CompOrgEmplEntity  compOrgEmplEntity = compOrgEmplService.getByUserId(compId,userId);
        //审批实例
        SwfProcInstEntity swfProcInstEntity = baseDao.getSwfProcInstById(
                aprvProcId);
        swfProcInstEntity.setUpdater(userId);
        swfProcInstEntity.setUpdateDate(nowDate);
        swfProcInstEntity.setAprvEndDate(nowDate);
        swfProcInstEntity.setAprvUserId(userId);
        swfProcInstEntity.setAprvStatus(SwfAprvEnum.APRVSTATUSED.value());
        swfProcInstEntity.setAprvEndResult(AprvResultEnum.APPROVALREFUSE.value());
        swfProcInstEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        swfProcInstEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
        baseDao.updateSwfProcInst(swfProcInstEntity);
        //审批任务
        SwfTaskEntity swfTaskEntity =
                swfTaskService.getSwfTaskByApplByprocInstId(compId, aprvProcId);
        swfTaskEntity.setUpdateDate(nowDate);
        swfTaskEntity.setUpdater(userId);
        swfTaskEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        swfTaskEntity.setStatus(SwfTaskEnum.NORMAL.value());
        swfTaskEntity.setIsLastAprv(SwfTaskEnum.NORMAL.value());
        swfTaskEntity.setAprvComment(aprvComment);
        swfTaskEntity.setApplSeqNum(swfProcInstEntity.getApplSeqNum());
        swfTaskEntity.setAprvUserId(userId);
        swfTaskEntity.setAprvDate(nowDate);
        swfTaskEntity.setAprvResult(SwfAprvEnum.APRVSTATUSED.value());
        swfTaskEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
        swfTaskService.updateSwfTask(swfTaskEntity);
        //查询审批名称
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = swfPhaseNameDefService.getSwfPhaseNameById(swfTaskEntity.getPhaseId());
        //审批履历
        SwfHstAprvEntity swfHstAprvEntity = new SwfHstAprvEntity();
        swfHstAprvEntity.setApplId(applId);
        swfHstAprvEntity.setDealDate(nowDate);
        swfHstAprvEntity.setCompId(compId);
        swfHstAprvEntity.setProcInstId(swfProcInstEntity.getId());
        swfHstAprvEntity.setDealUserId(userId);
        swfHstAprvEntity.setPhaseName(swfPhaseNameDefEntity.getName());
        swfHstAprvEntity.setDealType(SwfAprvDealTypeEnum.APRVREFUSE.value());
        swfHstAprvEntity.setAprvResult(SwfAprvDealTypeEnum.APRVCANCEL.value());
        swfHstAprvEntity.setAprvComment(aprvComment);
        swfHstAprvEntity.setDealEmplName(compOrgEmplEntity.getRealName());
        swfHstAprvEntity.setAprvTaskId(swfTaskEntity.getId());
        swfHstAprvEntity.setCreator(SecurityUser.getUser().getId());
        swfHstAprvEntity.setCreateDate(nowDate);
        swfHstAprvEntity.setIsLastAprv(SwfAprvDealTypeEnum.APRVCOMMIT.value());
        swfHstAprvEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        swfHstAprvEntity.setUpdateDate(nowDate);
        swfHstAprvEntity.setUpdater(SecurityUser.getUser().getId());
        swfHstAprvService.insert(swfHstAprvEntity);

        return swfProcInstEntity;
    }


    private void agree(Long compId ,Long aprvProcId,Long phaseId,Long userId,String aprvComment,Long applId,Long applUserId,String docType){
        Date nowDate = new Date();
        CompOrgEmplEntity  compOrgEmplEntity = compOrgEmplService.getByUserId(compId,userId);
            //找出公司设置差旅申请的审批段定义最后一段
            SwfProcPhaseEntity swfProcPhaseEntity = swfProcPhaseService.getActHeadLast(compId,aprvProcId);
            //判断是否为最终审
        //流程实例
        SwfProcInstEntity swfProcInstEntity = baseDao.getSwfProcInstById(
                aprvProcId);
            if (phaseId.equals(swfProcPhaseEntity.getPhaseId())) {
                SwfPhaseNameDefEntity swfPhaseNameDefEntity = swfPhaseNameDefService.getSwfPhaseNameById(
                        phaseId);

                swfProcInstEntity.setUpdater(userId);
                swfProcInstEntity.setUpdateDate(nowDate);
                swfProcInstEntity.setAprvEndDate(nowDate);
                swfProcInstEntity.setAprvUserId(userId);
                swfProcInstEntity.setAprvStatus(AprvStatusEnum.COMPLETEAPPROVAL.value());
                swfProcInstEntity.setAprvEndResult(AprvResultEnum.APPROVALAGREE.value());
                swfProcInstEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                swfProcInstEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
                baseDao.updateSwfProcInst(swfProcInstEntity);
                //审批任务
                SwfTaskEntity swfTaskEntity =
                        swfTaskService.getSwfTaskByApplByprocInstId(compId, aprvProcId);
                swfTaskEntity.setUpdateDate(nowDate);
                swfTaskEntity.setUpdater(userId);
                swfTaskEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                swfTaskEntity.setAprvDate(nowDate);
                swfTaskEntity.setIsPhaseLastAprv(LastApproveEnum.YES.value());
                swfTaskEntity.setAprvResult(SwfAprvEnum.APRVSTATUSING.value());
                swfTaskEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
                swfTaskEntity.setAprvComment(aprvComment);
                swfTaskEntity.setIsLastAprv(LastApproveEnum.YES.value());
                swfTaskService.updateSwfTask(swfTaskEntity);

                //审批履历
                SwfHstAprvEntity swfHstAprvEntity = new SwfHstAprvEntity();
                swfHstAprvEntity.setApplId(applId);
                swfHstAprvEntity.setDealDate(nowDate);
                swfHstAprvEntity.setPhaseName(swfPhaseNameDefEntity.getName());
                swfHstAprvEntity.setCompId(compId);
                swfHstAprvEntity.setProcInstId(swfProcInstEntity.getId());
                swfHstAprvEntity.setAprvComment(aprvComment);
                swfHstAprvEntity.setDealUserId(userId);
                swfHstAprvEntity.setDealEmplName(compOrgEmplEntity.getRealName());
                swfHstAprvEntity.setDealType(SwfAprvDealTypeEnum.APRVAGREED.value());
                swfHstAprvEntity.setAprvComment(aprvComment);
                swfHstAprvEntity.setIsPhaseLastAprv(LastApproveEnum.YES.value());
                swfHstAprvEntity.setAprvResult(AprvResultEnum.APPROVALAGREE.value());
                swfHstAprvEntity.setAprvTaskId(swfTaskEntity.getId());
                swfHstAprvEntity.setIsLastAprv(LastApproveEnum.YES.value());
                swfHstAprvEntity.setCreateDate(nowDate);
                swfHstAprvEntity.setCreator(SecurityUser.getUser().getId());
                swfHstAprvEntity.setUpdateDate(nowDate);
                swfHstAprvEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                swfHstAprvService.insert(swfHstAprvEntity);


                // 发送消息
//                sendMessageUtil.sendAgreeMessage(compId,applUserId, userId,
//                        docType, compOrgEmplEntity.getRealName(),
//                        applId);
                //否则段终审
            } else {
                SwfProcPhaseEntity swfProcPhaseEntity1 =
                        swfProcPhaseService.getActHeadByNow(compId, phaseId, aprvProcId);
                //找出当前审批段名字
                SwfPhaseNameDefEntity swfPhaseNameDefEntity1 = swfPhaseNameDefService.getSwfPhaseNameById(
                        phaseId);
                //找出当前段的下个审批段
                SwfProcPhaseEntity swfProcPhaseEntity2
                        = swfProcPhaseService.getActHeadNextBySort(compId, aprvProcId, swfProcPhaseEntity1.getSortNum() + 1);
                //找出下个审批段的名称
                SwfPhaseNameDefEntity swfPhaseNameDefEntity = swfPhaseNameDefService.getSwfPhaseNameById(
                        swfProcPhaseEntity2.getPhaseId());
                // 审批任务
                SwfTaskEntity swfTaskEntity =
                        swfTaskService.getSwfTaskByApplByprocInstId(compId, aprvProcId);
                swfTaskEntity.setUpdateDate(nowDate);
                swfTaskEntity.setUpdater(userId);
                swfTaskEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                swfTaskEntity.setAprvDate(nowDate);
                swfTaskEntity.setIsPhaseLastAprv(LastApproveEnum.YES.value());
                swfTaskEntity.setAprvResult(SwfAprvEnum.APRVSTATUSING.value());
                swfTaskEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
                swfTaskEntity.setAprvComment(aprvComment);
                swfTaskService.updateSwfTask(swfTaskEntity);
                //审批履历
                SwfHstAprvEntity swfHstAprvEntity = new SwfHstAprvEntity();
                swfHstAprvEntity.setApplId(applId);
                swfHstAprvEntity.setDealDate(nowDate);
                swfHstAprvEntity.setCompId(compId);
                swfHstAprvEntity.setProcInstId(swfProcInstEntity.getId());
                swfHstAprvEntity.setPhaseName(swfPhaseNameDefEntity1.getName());
                swfHstAprvEntity.setDealUserId(userId);
                swfHstAprvEntity.setDealType(SwfAprvDealTypeEnum.APRVAGREED.value());
                swfHstAprvEntity.setAprvResult(AprvResultEnum.APPROVALAGREE.value());
                swfHstAprvEntity.setIsPhaseLastAprv(LastApproveEnum.YES.value());
                swfHstAprvEntity.setAprvComment(aprvComment);
                swfHstAprvEntity.setDealEmplName(compOrgEmplEntity.getRealName());
                swfHstAprvEntity.setAprvTaskId(swfTaskEntity.getId());
                swfHstAprvEntity.setCreateDate(nowDate);
                swfHstAprvEntity.setCreator(SecurityUser.getUser().getId());
                swfHstAprvEntity.setUpdateDate(nowDate);
                swfHstAprvEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                swfHstAprvService.insert(swfHstAprvEntity);
                    //审批任务
                    SwfTaskEntity swfTaskEntity1 = new SwfTaskEntity();
                    swfTaskEntity1.setStatus(SwfTaskEnum.NORMAL.value());
                    swfTaskEntity1.setAprvUserId(swfProcPhaseEntity2.getUserId());
                    swfTaskEntity1.setCompId(compId);
                    swfTaskEntity1.setUpdateDate(nowDate);
                    swfTaskEntity1.setProcInstId(swfProcInstEntity.getId());
                    swfTaskEntity1.setApplSeqNum(swfProcInstEntity.getApplSeqNum());
                    swfTaskEntity1.setPhaseId(swfPhaseNameDefEntity.getId());
                    swfTaskEntity1.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                    swfTaskEntity1.setUpdater(SecurityUser.getUser().getId());
                    swfTaskEntity1.setCreateDate(nowDate);
                    swfTaskService.insert(swfTaskEntity1);

//                sendMessageUtil.submitSendMessage(compOrgEmplEntityS.getRealName(), spdComApplEntity.getIsAgentAppl(), compOrgEmplEntityS.getRealName(),
//                        spdPayHeadAgreedDTO.getAprvUserId(), spdComApplEntity.getApplUserId(),
//                        spdPayHeadAgreedDTO.getCompId(), spdComApplEntity.getApplDeptName(),
//                        DocTypeNameEnum.SPD_PAY_HEAD.value(), spdComApplEntity.getApplDocNum());
            }


    }



    /**
     * @Author: Ding Ya Yong
     * @Description: 撤回
     * @Date: 2022/1/12 20:53
     * @Param: [compId, applUserId, aprvProcId, applId]
     * @return: void
     **/
//    @Transactional(rollbackFor = Exception.class)
    private SwfProcInstEntity withdraw(Long compId,Long applUserId,Long aprvProcId,Long applId){
        Date nowDate = new Date();
        //审批部分
        //审批任务
        SwfTaskEntity swfTaskEntity =
                swfTaskService.getSwfTaskByCreator(compId,
                        applUserId, aprvProcId);
        swfTaskEntity.setUpdateDate(nowDate);
        swfTaskEntity.setUpdater(applUserId);
        swfTaskEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        swfTaskEntity.setStatus(SwfTaskEnum.REVOCATION.value());
        swfTaskEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
        swfTaskService.updateSwfTask(swfTaskEntity);
        //流程实例
        SwfProcInstEntity swfProcInstEntity = baseDao.getSwfProcInstById(
                aprvProcId);
        swfProcInstEntity.setAprvStatus(SwfAprvEnum.APRVSTATUSDIS.value());
        swfProcInstEntity.setUpdater(applUserId);
        swfProcInstEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
        swfProcInstEntity.setIsDeleted(SwfTaskEnum.REVOCATION.value());
        baseDao.updateSwfProcInst(swfProcInstEntity);
        CompSysUserEmplEntity compSysUserEmplEntity
                = compSysUserEmplService.findCompEmpl(compId, applUserId);
        CompOrgEmplEntity compOrgEmplEntity = compOrgEmplService.findById(compSysUserEmplEntity.getEmplId());
        //审批履历
        SwfHstAprvEntity swfHstAprvEntity = new SwfHstAprvEntity();
        swfHstAprvEntity.setApplId(applId);
        swfHstAprvEntity.setDealDate(nowDate);
        swfHstAprvEntity.setCompId(compId);
        swfHstAprvEntity.setProcInstId(swfProcInstEntity.getId());
        SwfPhaseNameDefEntity swfPhaseNameDefEntity = swfPhaseNameDefService.getSwfPhaseNameById(swfTaskEntity.getPhaseId());
        if (swfPhaseNameDefEntity != null) {
            swfHstAprvEntity.setPhaseName(swfPhaseNameDefEntity.getName());
        }
        swfHstAprvEntity.setDealEmplName(compOrgEmplEntity.getRealName());
        swfHstAprvEntity.setDealUserId(applUserId);
        swfHstAprvEntity.setDealType(SwfAprvDealTypeEnum.APRVCANCEL.value());
        swfHstAprvEntity.setUpdateDate(nowDate);
        swfHstAprvEntity.setUpdater(SecurityUser.getUser().getId());
        swfHstAprvEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        swfHstAprvEntity.setCreateDate(nowDate);
        swfHstAprvEntity.setCreator(SecurityUser.getUser().getId());
        swfHstAprvService.insert(swfHstAprvEntity);

        return swfProcInstEntity;
    }

    /**
     * @Author: Ding Ya Yong
     * @Description: 提交
     * @Date: 2022/1/5 21:15
     * @Param: []
     * @return: void
     **/
//    @Transactional(rollbackFor = Exception.class)
    private SwfProcInstEntity  startWorkFlow(Long compId,Long applId,String docType,Long applUserId){
        // 现在时间
        Date nowDate = new Date();
        // 找出申请人对应的部门
//找出用户对应的员工
        CompOrgEmplEntity  compOrgEmplEntity = compOrgEmplService.getByUserId(compId,applUserId);
        //创建实例
        SwfProcInstEntity swfProcInstEntity = new SwfProcInstEntity();
        swfProcInstEntity.setApplDate(nowDate);
        swfProcInstEntity.setCompId(compId);
        swfProcInstEntity.setApplMakerUserId(applUserId);
        swfProcInstEntity.setApplUserId(applUserId);
        swfProcInstEntity.setApplEmplId(compOrgEmplEntity.getId());
        swfProcInstEntity.setApplSeqNum(1);
        swfProcInstEntity.setApplId(applId);
        swfProcInstEntity.setAprvStatus(SwfAprvEnum.APRVSTATUSING.value());
        swfProcInstEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        baseDao.insert(swfProcInstEntity);
        //找出该公司对应的费用报销报销单审批设置
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities = swfDocPhaseDefService.getDocPhaseDefListByDocType(compId, docType);
        if (swfDocPhaseDefEntities.size() > 0) {
            //将单据审批设置添加至流程实例审批段设置表中
            for (SwfDocPhaseDefEntity swfDocPhaseDefEntity : swfDocPhaseDefEntities) {
                SwfProcPhaseEntity swfProcPhaseEntity = new SwfProcPhaseEntity();
                swfProcPhaseEntity.setPhaseId(swfDocPhaseDefEntity.getPhaseId());
                swfProcPhaseEntity.setProcInstId(swfProcInstEntity.getId());
                swfProcPhaseEntity.setCompId(compId);
                swfProcPhaseEntity.setSortNum(swfDocPhaseDefEntity.getSortNum());
                swfProcPhaseEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                swfProcPhaseService.insert(swfProcPhaseEntity);
            }
            //找出当前公司和审批实例对应的第一个审批定义
            SwfProcPhaseEntity swfProcPhaseEntity = swfProcPhaseService.getActHeadFristSort(compId, swfProcInstEntity.getId());
            if (swfProcPhaseEntity != null) {
                //审批任务
                SwfTaskEntity swfTaskEntity = new SwfTaskEntity();
                swfTaskEntity.setCompId(compId);
                swfTaskEntity.setUpdateDate(nowDate);
                swfTaskEntity.setApplSeqNum(SwfAprvEnum.APRVSTATUSING.value());
                swfTaskEntity.setPhaseId(swfProcPhaseEntity.getPhaseId());
                swfTaskEntity.setProcInstId(swfProcInstEntity.getId());
                swfTaskEntity.setStatus(SwfTaskEnum.NORMAL.value());
                swfTaskEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                swfTaskEntity.setAprvUserId(swfProcPhaseEntity.getUserId());
                swfTaskService.insert(swfTaskEntity);
                //审批履历
                SwfHstAprvEntity swfHstAprvEntity = new SwfHstAprvEntity();
                swfHstAprvEntity.setApplId(applId);
                swfHstAprvEntity.setCompId(compId);
                swfHstAprvEntity.setProcInstId(swfProcInstEntity.getId());
                swfHstAprvEntity.setDealDate(nowDate);
                swfHstAprvEntity.setAprvTaskId(swfTaskEntity.getId());
                swfHstAprvEntity.setDealUserId(applUserId);
                swfHstAprvEntity.setDealEmplName(compOrgEmplEntity.getRealName());
                swfHstAprvEntity.setDealType(SwfAprvDealTypeEnum.APRVCOMMIT.value());
                swfHstAprvEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                SwfPhaseNameDefEntity swfPhaseNameDefEntity = swfPhaseNameDefService.getSwfPhaseNameById(swfProcPhaseEntity.getPhaseId());
                if (swfPhaseNameDefEntity != null) {
                    swfHstAprvEntity.setPhaseName(swfPhaseNameDefEntity.getName());
                }
                swfHstAprvService.insert(swfHstAprvEntity);
                //保存下一审批人
                SwfNextAprvDefEntity swfNextAprvDefEntitys =
                        swfNextAprvDefService.getNextAprvtByDocType(applUserId, compId, docType);
                if (swfNextAprvDefEntitys == null) {
                    SwfNextAprvDefEntity swfNextAprvDefEntity = new SwfNextAprvDefEntity();
                    swfNextAprvDefEntity.setCompId(compId);
                    swfNextAprvDefEntity.setDocTypeValue(docType);
                    swfNextAprvDefEntity.setNextAprvPostId(swfProcPhaseEntity.getPosnId());
                    swfNextAprvDefEntity.setUpdateDate(nowDate);
                    swfNextAprvDefEntity.setUpdater(SecurityUser.getUser().getId());
                    swfNextAprvDefEntity.setUserId(applUserId);
                    swfNextAprvDefEntity.setNextAprvUserId(swfProcPhaseEntity.getUserId());
                    swfNextAprvDefEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                    swfNextAprvDefEntity.setCreateDate(nowDate);
                    swfNextAprvDefEntity.setCreator(SecurityUser.getUser().getId());
                    swfNextAprvDefService.insert(swfNextAprvDefEntity);
                } else {
                    swfNextAprvDefEntitys.setIsDeleted(SwfTaskEnum.REVOCATION.value());
                    swfNextAprvDefEntitys.setNextAprvUserId(swfProcPhaseEntity.getUserId());
                    swfNextAprvDefEntitys.setUpdater(SecurityUser.getUser().getId());
                    swfNextAprvDefEntitys.setUpdateDate(nowDate);
                    swfNextAprvDefEntitys.setNextAprvPostId(swfProcPhaseEntity.getPosnId());
                    swfNextAprvDefEntitys.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
                    swfNextAprvDefService.updateSwfNextAprvDef(swfNextAprvDefEntitys);
                }
            }
        }
        return swfProcInstEntity;
    }
}
